package com.fyk.reaml;

import com.fyk.entity.User;
import com.fyk.mapper.UserMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author qazws
 * @Date 2022/8/19 17:02
 * @Version 1.0
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserMapper userMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

        String username = token.getPrincipal()+"";

        User user = userMapper.selectUserByUsername(username);

        if (user == null){
            throw new UnknownAccountException();
        }
        return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),this.getName());
    }
}
